package com.github.cakin.shiro.chapter3.permission;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.PermissionResolver;
import org.apache.shiro.authz.permission.WildcardPermission;

/**
 * @className: BitAndWildPermissionResolver
 * @description: 将字符串类型的权限解析为BitPermission或WildcardPermission
 * @date: 2020/5/17
 * @author: cakin
 */
public class BitAndWildPermissionResolver implements PermissionResolver {
    /**
     * 功能描述：根据权限字符串是否以 “+” 开头来解析权限字符串为 BitPermission 或 WildcardPermission
     *
     * @param permissionString 权限字符串
     * @return Permission 权限对象
     * @author cakin
     * @date 2020/5/17
     * @description: 该函数被触发时刻
     * resolvePermission:27, BitAndWildPermissionResolver (com.github.cakin.shiro.chapter3.permission)
     * isPermitted:456, AuthorizingRealm (org.apache.shiro.realm)
     * isPermitted:223, ModularRealmAuthorizer (org.apache.shiro.authz)
     * isPermitted:113, AuthorizingSecurityManager (org.apache.shiro.mgt)
     * isPermitted:158, DelegatingSubject (org.apache.shiro.subject.support)
     * testIsPermitted:52, AuthorizerTest (com.github.cakin.shiro.chapter3)
     */
    public Permission resolvePermission( String permissionString ) {
        if (permissionString.startsWith("+")) {
            return new BitPermission(permissionString);
        }
        return new WildcardPermission(permissionString);
    }
}
